import sys
import time
import json
from datetime import datetime,timedelta
from selenium import webdriver
from selenium.common import exceptions
from selenium.webdriver.chrome.options import Options
import urllib.parse
from mongoengine import *
from selenium.webdriver.common.action_chains import ActionChains
from qcloud_cos import CosConfig
from qcloud_cos import CosS3Client
import logging

whiteList = ['-101','-100','4450443968889831','4044671657000761','3263330414','5581785513']

logging.basicConfig(level=logging.INFO, stream=sys.stdout)
secret_id = 'AKIDFH7vXN4WtIsGXbsWMxLaaV3QsGQNsRTD'      # 替换为用户的 secretId
secret_key = 'I1k6ldHhFGF0ZzMWSMvMuGnIdrwrWQDD'      # 替换为用户的 secretKey
region = 'ap-shanghai'     # 替换为用户的 Region
config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key)
# 2. 获取客户端对象
client = CosS3Client(config)




class privateuser(Document):
    nickname = StringField()
    gender = IntField()
    city = StringField()
    followerCount = IntField()
    uid = StringField()
    addTime = DateTimeField()
    accountUid = StringField()
    accountName = StringField()
    updatetime = DateTimeField()
    postCount = IntField()
    fansCount = IntField()
    searched = IntField()
    privateList = ListField()
    isFans = IntField()
    isClicked = IntField()
    isDelete = IntField()
    firstchprivatedate = DateTimeField()
    meta = {
        'strict':False
    }


class newprivateuser(Document):
    nickname = StringField()
    gender = IntField()
    city = StringField()
    followerCount = IntField()
    uid = StringField()
    addTime = DateTimeField()
    accountUid = StringField()
    accountName = StringField()
    updatetime = DateTimeField()
    postCount = IntField()
    fansCount = IntField()
    searched = ListField()
    firstClickUser = ListField()
    allClickUser = ListField()
    isClicked = IntField()
    isDelete = IntField()
    meta = {
        'strict':False
    }


class myprivateaccounts(Document):
    name = StringField()
    uid = StringField()
    addTime = DateTimeField()
    updatetime = DateTimeField()
    isDelete = IntField()
    meta = {
        'strict':False
    }


class chatlistuser(Document):
    nickname = StringField()
    gender = IntField()
    city = StringField()
    followerCount = IntField()
    uid = StringField()
    addTime = DateTimeField()
    accountUid = StringField()
    accountName = StringField()
    updatetime = DateTimeField()
    postCount = IntField()
    fansCount = IntField()
    searched = ListField()
    firstClickUser = ListField()
    allClickUser = ListField()
    isClicked = IntField()
    isDelete = IntField()
    lastShowDate = DateTimeField()
    error = IntField()
    meta = {
        'strict':False
    }


class showchatlist(Document):
    accountName = StringField()
    uidlist = ListField()
    isDelete = IntField()
    updatetime = DateTimeField()
    needsearchlist = ListField()
    meta = {
        'strict':False
    }


class refreshuserchat(Document):
    isDelete = IntField()
    addTime = DateTimeField()
    updatetime = DateTimeField()
    uid = StringField()
    searched = ListField()
    accountUid = StringField()
    accountName = StringField()
    meta = {
        'strict':False
    }

def NewDriver(accountName):
    chrome_options = Options()
    chrome_options.add_argument("user-data-dir=" + path2 + 'searchprivate' + accountName)
    driver = webdriver.Chrome(path, options=chrome_options)
    driver.maximize_window()
    return driver

def searchprivate(thisdriver,user):
    # thisdriver.find_element_by_link_text('私信').click()
    chaturl = " https://api.weibo.com/chat/#/chat?to_uid={UID}"
    thisdriver.get(chaturl.format(UID=user.uid))
    time.sleep(3)
    chatDetailList = []
    firstchprivatedate = datetime.now()
    chatBox = thisdriver.find_elements_by_class_name("chat-content")
    if len(chatBox) != 0:
        try:
            chatBox.click()
            chatBox.sendKeys(Keys.HOME)
            chatBox.sendKeys(Keys.HOME)
        except:
            pass
        chatList = chatBox[0].find_elements_by_tag_name("li")
        year = datetime.strftime(datetime.now(),'%Y')
        datestr = datetime.strftime(datetime.now(),'%Y-%m-%d')
        for index,item in enumerate(chatList):
            sendUser = ""
            if len(item.find_elements_by_class_name("me")) !=0:
                sendUser = "me"
            elif len(item.find_elements_by_class_name("other")) !=0:
                sendUser = "other"
            try:
                date = item.find_element_by_class_name("time").get_attribute('textContent').replace(" ","").replace("\n","")
                if date[4] == "-":
                    if firstchprivatedate == "":
                        firstchprivatedate = datetime.strptime(date, "%Y-%m-%d")
                elif date[2] == "-":
                    if firstchprivatedate == "":
                        firstchprivatedate = datetime.strptime(year+date, "%Y%m-%d%H:%M")
                elif "昨天" in date:
                    if firstchprivatedate == "":
                        firstchprivatedate = datetime.now()+timedelta(days=-1)
                else:
                    if firstchprivatedate == "":
                        firstchprivatedate = datetime.strptime(datestr+date, "%Y-%m-%d%H:%M")
            except:
                date = ""
            try:
                text = item.find_element_by_class_name("puretext").get_attribute('textContent')
            except:
                text = ""
            image = ""
            try:
                item.find_element_by_class_name("large_img_container").click()
                time.sleep(2)
                # test = thisdriver.find_element_by_class_name("viewer-canvas").get_screenshot_as_png()
                check = thisdriver.get_screenshot_as_file(user.uid + "_" + str(index) + ".jpg")
                if check:
                    response = client.upload_file(
                    Bucket='tvgweibo-1305078098',
                    LocalFilePath='./' + user.uid + "_" + str(index) + ".jpg",
                    Key=user.uid + "_" + str(index) + ".jpg",
                    PartSize=1,
                    MAXThread=10,
                    EnableMD5=False
                    )
                image = "https://tvgweibo-1305078098.cos.ap-shanghai.myqcloud.com/" + user.uid + "_" + str(index) + ".jpg"
            except Exception as e:
                # print("图片下载失败",print(str(e)))
                pass
            chatDetailList.append({"date":date,"text":text,"sendUser":sendUser,"image":image})
    # print(firstchprivatedate)
    return chatDetailList,firstchprivatedate


def searchprivatebyuid(thisdriver,user):
    # thisdriver.find_element_by_link_text('私信').click()
    chaturl = " https://api.weibo.com/chat/#/chat?to_uid={UID}"
    thisdriver.get(chaturl.format(UID=user))
    time.sleep(3)
    chatDetailList = []
    firstchprivatedate = datetime.now()
    chatBox = thisdriver.find_elements_by_class_name("chat-content")
    if len(chatBox) != 0:
        try:
            chatBox.click()
            chatBox.sendKeys(Keys.HOME)
            chatBox.sendKeys(Keys.HOME)
        except:
            pass
        chatList = chatBox[0].find_elements_by_tag_name("li")
        year = datetime.strftime(datetime.now(),'%Y')
        datestr = datetime.strftime(datetime.now(),'%Y-%m-%d')
        for index,item in enumerate(chatList):
            sendUser = ""
            if len(item.find_elements_by_class_name("me")) !=0:
                sendUser = "me"
            elif len(item.find_elements_by_class_name("other")) !=0:
                sendUser = "other"
            try:
                date = item.find_element_by_class_name("time").get_attribute('textContent').replace(" ","").replace("\n","")
                if date[4] == "-":
                    if firstchprivatedate == "":
                        firstchprivatedate = datetime.strptime(date, "%Y-%m-%d")
                elif date[2] == "-":
                    if firstchprivatedate == "":
                        firstchprivatedate = datetime.strptime(year+date, "%Y%m-%d%H:%M")
                elif "昨天" in date:
                    if firstchprivatedate == "":
                        firstchprivatedate = datetime.now()+timedelta(days=-1)
                else:
                    if firstchprivatedate == "":
                        firstchprivatedate = datetime.strptime(datestr+date, "%Y-%m-%d%H:%M")
            except:
                date = ""
            try:
                text = item.find_element_by_class_name("puretext").get_attribute('textContent')
            except:
                text = ""
            image = ""
            try:
                item.find_element_by_class_name("large_img_container").click()
                time.sleep(2)
                # test = thisdriver.find_element_by_class_name("viewer-canvas").get_screenshot_as_png()
                check = thisdriver.get_screenshot_as_file(user + "_" + str(index) + ".jpg")
                if check:
                    response = client.upload_file(
                    Bucket='tvgweibo-1305078098',
                    LocalFilePath='./' + user + "_" + str(index) + ".jpg",
                    Key=user + "_" + str(index) + ".jpg",
                    PartSize=1,
                    MAXThread=10,
                    EnableMD5=False
                    )
                image = "https://tvgweibo-1305078098.cos.ap-shanghai.myqcloud.com/" + user + "_" + str(index) + ".jpg"
            except Exception as e:
                # print("图片下载失败",print(str(e)))
                pass
            chatDetailList.append({"date":date,"text":text,"sendUser":sendUser,"image":image})
    return chatDetailList,firstchprivatedate
   

def searchDriver0(user):
    userUrl = 'https://weibo.com/u/{UID}?refer_flag=1005050005_&is_all=1'
    urlWithUid = userUrl.format(UID=user.uid)
    if urlWithUid == 'https://weibo.com/u/?refer_flag=1005050005_&is_all=1':
        print(1+a)
    driver0.get(urlWithUid)
    # try:
    moreMenu = driver0.find_element_by_class_name('W_btn_pf_menu')
    action = ActionChains(driver0)
    action.move_to_element_with_offset(moreMenu, 0, 100).perform()
    action.move_to_element(moreMenu).perform()
    time.sleep(1)
    deleteFans = driver0.find_elements_by_link_text('移除粉丝')
    if len(deleteFans) != 0:
        # tempuser = privateuser.objects(uid = user.uid,accountUid = accountList[0]["accountUid"])
        tempuser = privateuser.objects(uid = user.uid,accountUid = accountList[index]["uid"])
        privateChat,firstchprivatedate = searchprivate(driver0,user)
        # print(privateChat)
        if len(tempuser) != 0:
            tempuser[0].isFans = 1
            tempuser[0].privateList = privateChat
            tempuser[0].save()
        else:
            newdata = privateuser(
                nickname = user.nickname,
                gender = user.gender,
                city = user.city,
                followerCount = user.followerCount,
                fansCount = user.fansCount,
                postCount = user.postCount,
                uid = user.uid,
                searched = 0,
                addTime = datetime.now(),
                accountUid = accountList[index]["uid"],
                accountName = accountList[index]["name"],
                updatetime = datetime.now(),
                isFans = 1,
                privateList = privateChat,
                isDelete = 0,
                firstchprivatedate = firstchprivatedate
            )
            newdata.save()
    else:
        tempuser = privateuser.objects(uid = user.uid,accountUid = accountList[index]["uid"])
        privateChat,firstchprivatedate = searchprivate(driver0,user)
        if len(tempuser) != 0:
            tempuser[0].isFans = 0
            tempuser[0].save()
        else:
            newdata = privateuser(
                nickname = user.nickname,
                gender = user.gender,
                city = user.city,
                followerCount = user.followerCount,
                fansCount = user.fansCount,
                postCount = user.postCount,
                uid = user.uid,
                searched = 0,
                addTime = datetime.now(),
                accountUid = accountList[index]["uid"],
                accountName = accountList[index]["name"],
                updatetime = datetime.now(),
                privateList = privateChat,
                isFans = 0,
                isDelete = 0,
                firstchprivatedate = firstchprivatedate
            )
            newdata.save()
    # except:
    #     user.error = 1
    #     user.save()



def searchDriverupdate0(user):
    tempuser = privateuser.objects(uid = user,accountUid = accountList[index]["uid"])
    if len(tempuser) != 0:
        print("1",user)
        # userUrl = 'https://weibo.com/u/{UID}?refer_flag=1005050005_&is_all=1'
        # urlWithUid = userUrl.format(UID=user)
        # if urlWithUid == 'https://weibo.com/u/?refer_flag=1005050005_&is_all=1':
        #     print(1+a)
        # driver0.get(urlWithUid)
        # moreMenu = driver0.find_element_by_class_name('W_btn_pf_menu')
        # action = ActionChains(driver0)
        # action.move_to_element_with_offset(moreMenu, 0, 100).perform()
        # action.move_to_element(moreMenu).perform()
        # time.sleep(1)
        # deleteFans = driver0.find_elements_by_link_text('移除粉丝')
        updateuser = privateuser.objects.get(uid = user,accountUid = accountList[index]["uid"])
        privateChat,firstchprivatedate = searchprivatebyuid(driver0,user)
        # updateuser.isFans = 0
        updateuser.privateList = privateChat
        updateuser.save()
        # if len(deleteFans) != 0:
        #     privateChat,firstchprivatedate = searchprivatebyuid(driver0,user)
        #     # updateuser.isFans = 1
        #     updateuser.privateList = privateChat
        #     updateuser.save()
        # else:
        #     privateChat,firstchprivatedate = searchprivatebyuid(driver0,user)
        #     # updateuser.isFans = 0
        #     updateuser.privateList = privateChat
        #     updateuser.save()
    else:
        userUrl = 'https://weibo.com/u/{UID}?refer_flag=1005050005_&is_all=1'
        urlWithUid = userUrl.format(UID=user)
        if urlWithUid == 'https://weibo.com/u/?refer_flag=1005050005_&is_all=1':
            print(1+a)
        driver0.get(urlWithUid)
        moreMenu = driver0.find_element_by_class_name('W_btn_pf_menu')
        action = ActionChains(driver0)
        action.move_to_element_with_offset(moreMenu, 0, 100).perform()
        action.move_to_element(moreMenu).perform()
        time.sleep(1)
        nickname = driver0.find_element_by_class_name('username').get_attribute('textContent')
        deleteFans = driver0.find_elements_by_link_text('移除粉丝')
        privateChat,firstchprivatedate = searchprivatebyuid(driver0,user)
        if len(deleteFans) != 0:
            newdata = privateuser(
                nickname = nickname,
                # gender = user.gender,
                # city = user.city,
                # followerCount = user.followerCount,
                # fansCount = user.fansCount,
                # postCount = user.postCount,
                uid = user,
                searched = 0,
                addTime = datetime.now(),
                accountUid = accountList[index]["uid"],
                accountName = accountList[index]["name"],
                updatetime = datetime.now(),
                isFans = 1,
                privateList = privateChat,
                isDelete = 0,
                firstchprivatedate = firstchprivatedate
            )
            newdata.save()
        else:
            newdata = privateuser(
                nickname = nickname,
                # gender = user.gender,
                # city = user.city,
                # followerCount = user.followerCount,
                # fansCount = user.fansCount,
                # postCount = user.postCount,
                uid = user,
                searched = 0,
                addTime = datetime.now(),
                accountUid = accountList[index]["uid"],
                accountName = accountList[index]["name"],
                updatetime = datetime.now(),
                privateList = privateChat,
                isFans = 0,
                isDelete = 0,
                firstchprivatedate = firstchprivatedate
            )
            newdata.save()




if __name__ == '__main__':
    index = int(sys.argv[1])
    # accountName = sys.argv[1]
    # accountUid = sys.argv[2]
    # accountList = [{"accountUid":accountUid,"accountName":accountName}]
    mode = 1
    path = 'd:/work/weiboprivate/python/chromedriver.exe'
    path2 = 'd:/work/weiboprivate/profile/'
    if mode == 1:
        path = 'd:/weiboprivate/python/chromedriver.exe'
        path2 = 'd:/weiboprivate/profile/'
        connect('weibo', host='1.15.92.12', port=27817)
        # connect('weibo', host='localhost', port=27017)
    else:
        connect('weibo', host='1.15.92.12', port=27817)
        # connect('weibo', host='localhost', port=27017)

    accountList = myprivateaccounts.objects(isDelete=0)
    accountName = accountList[index]['name']
    accountUid = accountList[index]['uid']
    
    driver0 = NewDriver(accountName)
    driver0.get("https://weibo.com")


    
    while True:
        time.sleep(1)
        tempchatlist = showchatlist.objects.get().uidlist
        userList = chatlistuser.objects(searched__nin=[accountList[index]['name']],uid__in=tempchatlist).order_by("-_id")
        # print(len(userList))
        if len(userList) == 0:
            userList = chatlistuser.objects(searched__nin=[accountList[index]['name']],uid__nin=whiteList).order_by("-_id")[:10]
            # userList = chatlistuser.objects(uid="2358141671").order_by("-_id")[:10]
            for user in userList:
                if user.uid not in whiteList:
                    try:
                        searchDriver0(user)
                        # user.update_one(add_to_set__searched=accountList[index]['name'])
                        user.searched.append(accountList[index]['name'])
                        user.save()
                    except:
                        pass
        else:
            for user in userList:
                print("2",user.uid)
                if user.uid not in whiteList:
                    try:
                        searchDriver0(user)
                        # user.update_one(add_to_set__searched=accountList[index]['name'])
                        user.searched.append(accountList[index]['name'])
                        user.save()
                    except:
                        pass


    # while True:
    #     time.sleep(1)
    #     chatlist = showchatlist.objects.get()
    #     tempuserlist =  refreshuserchat.objects(searched__nin=[accountList[index]['name']]).order_by("-_id")
    #     print(tempuserlist)
    #     if len(tempuserlist) != 0:
    #         for item in tempuserlist:
    #             if item not in whiteList:
    #                 try:
    #                     searchDriverupdate0(item.uid)
    #                     items = privateuser.objects(uid=item.uid)
    #                     item.searched.append(accountList[index]['name'])
    #                     item.save()
    #                     # print(len(items))
    #                     # if len(items)>=4:
    #                     #     chatlist.needsearchlist.remove(item)
    #                     #     chatlist.save()
    #                 except:
    #                     pass

    #     else:
    #         print("notin")
    #         userList = chatlistuser.objects(searched__nin=[accountList[index]['name']],uid__nin=whiteList).order_by("-_id")[:10]
    #         # userList = chatlistuser.objects(uid="2358141671").order_by("-_id")[:10]
    #         for user in userList:
    #             if user.uid not in whiteList:
    #                 try:
    #                     searchDriver0(user)
    #                     # user.update_one(add_to_set__searched=accountList[index]['name'])
    #                     user.searched.append(accountList[index]['name'])
    #                     user.save()
    #                 except:
    #                     pass
    
        